package com.czl.module_notice.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.DialogHelper
import com.czl.module_notice.BR
import com.czl.module_notice.R
import com.czl.module_notice.adapter.NoticeMsgAdapter
import com.czl.module_notice.databinding.FragmentNoticeListBinding
import com.czl.module_notice.viewmodel.NoticeListViewModel

/**
 *
 * @Description:
 * @Author: XCH
 * @CreateDate: 2022/3/8 9:26
 */
@Route(path = AppConstants.Router.Notice.F_NOTICE_LIST)
class NoticeListFragment : BaseFragment<FragmentNoticeListBinding, NoticeListViewModel>() {

    private lateinit var mAdapter: NoticeMsgAdapter

    override fun initContentView(): Int {
        return R.layout.fragment_notice_list
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        if (arguments != null) {
            viewModel.isRead =
                requireArguments().getString(AppConstants.BundleKey.IS_READ_KEY).toString()
        }
        initAdapter()
        reload()
    }

    private fun initAdapter() {
        mAdapter = NoticeMsgAdapter(this)
        binding.ryCommon.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }
    }

    override fun initViewObservable() {
        LiveBusCenter.observeRefreshNoticeMsgEvent(this, {
            binding.smartCommon.autoRefresh()
        })
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            handleRecyclerviewData(
                data == null,
                data?.records as MutableList<*>?,
                mAdapter,
                binding.ryCommon,
                binding.smartCommon,
                viewModel.currentPage,
                data?.current == data?.pages
            )
        })

        //跳转待评价页面
        viewModel.uc.navScoreEvent.observe(this, {
            startContainerActivity(
                AppConstants.Router.Report.F_SERVICE_SCORE,
                Bundle().apply {
                    putString(AppConstants.BundleKey.WORK_ORDER_NO, it)
                })
        })

    }

    override fun reload() {
        super.reload()
        binding.smartCommon.autoRefresh()
    }

    override fun useBaseLayout(): Boolean {
        return false
    }
}